package
{
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.filters.ShaderFilter;
	
	public class PBGradient extends MovieClip
	{
		private var gradBit:MovieClip;
		
		private var loader:URLLoader;
		private var shaderURL:String = "PixelBender/hsv.pbj";
		
		private var pbShader:Shader;
		private var pbShaderFilter:ShaderFilter;
		
		public function PBGradient():void
		{
			gradBit = MovieClip(getChildAt(0));
			
			// Load in the PixelBender filter
			loader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onLoadComplete);
			loader.dataFormat = URLLoaderDataFormat.BINARY;
			loader.load(new URLRequest(shaderURL));
		}
		
		private function onLoadComplete (evt:Event):void
		{
			pbShader = new Shader(loader.data);
			
			pbShaderFilter = new ShaderFilter(pbShader);
						
			pbShader.data.hue.value = [180];
			pbShader.data.saturation.value = [-0.1];
			pbShader.data.brightness.value = [-0.1];
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		private function onEnterFrame (evt:Event):void
		{
			var newHueAmount:Number = pbShader.data.hue.value;
			if (newHueAmount >= 180) newHueAmount = -180;
			pbShader.data.hue.value = [newHueAmount + 1];
			
			gradBit.filters = [pbShaderFilter];
		}
	}
}